/**
 * @Description:最后sql后面要跟空格,where 后面跟上1+1 永远为true 这样 后面就可以跟 and了
 * @DescriptionLink:https://www.bilibili.com/video/BV1EZ4y1H7br?p=15
 * @author 麦田守望客
 * @date 2020/8/31
 **/

const stuDao = require("../dao/stuDao");
function formatData(data) {
  let rawData = JSON.stringify(data);
  let machiData = JSON.stringify(data);
  machiData = machiData.replace(/s_id/g, "学号");
  machiData = machiData.replace(/s_name/g, "姓名");
  machiData = machiData.replace(/s_age/g, "年龄");
  machiData = machiData.replace(/s_sex/g, "性别");
  machiData = machiData.replace(/s_date/g, "出生日期");
  machiData = machiData.replace(/s_phone/g, "联系电话");
  machiData = machiData.replace(/s_grade/g, "成绩");
  machiData = machiData.replace(/c_name/g, "班级");
  return machiData;
}

module.exports = {
  getStuData(req, res) {
    stuDao.stuDao(
      "SELECT s_id,s_name,s_age,s_sex,s_date,s_phone,s_grade,c_name FROM t_student JOIN t_class on s_cid = c_id order by s_id DESC",
      [],
      function (err, data) {
        if (data.length > 0) {
          data = formatData(data);
          res.send(data);
        }
      }
    );
  },
  delStu(req, res) {
    let { stuid } = req.query;
    stuDao.stuDao("delete from t_student where s_id=?", [stuid], function (
      err,
      data
    ) {
      if (data) {
        res.send("1");
      } else {
        res.send("0");
      }
    });
  },
  delStuPost(req, res) {
    let { stuid } = req.body;
    stuDao.stuDao("delete from t_student where s_id=?", [stuid], function (
      err,
      data
    ) {
      if (data) {
        res.send("1");
      } else {
        res.send("0");
      }
    });
  },
  searchStu(req, res) {
    let { name, sex, age } = req.body;
    let arr = [];
    let sql =
      "SELECT s_id,s_name,s_age,s_sex,s_date,s_phone,s_grade,c_name FROM t_student JOIN t_class on s_cid = c_id where 1=1 ";
    let order = "order by s_id";
    if (name != "") {
      sql = `${sql} and s_name like ? `;
      arr.push(name + "%");
    }
    if (sex != "") {
      sql = `${sql} and s_sex = ? `;
      arr.push(sex);
    }
    if (age != "") {
      sql = `${sql} and s_age = ? `;
      arr.push(Number.parseInt(age));
    }
    sql = sql + order;
    console.log(sql);
    stuDao.stuDao(sql, arr, function (err, data) {
      console.log(data);
      if (data) {
        //如果data为undefined代表数据库操作错误
        //如果data length为0 代表没有匹配数据
        data = formatData(data);
        res.send(data);
      } else {
        res.send("0");
      }
    });
  },
  getPage(req, res) {
    //limit 3 定死
    stuDao.stuDao("select count(*) as pageNum from t_student", [], function (
      err,
      data
    ) {
      if (data) {
        res.send(data);
      }
    });
  },
  getPageData(req, res) {
    let { pageNum, pageDataSize } = req.body;
    let arr = [];
    //0-3,3-6,6-9 : 0 3 6 9
    pageNum = (pageNum - 1) * pageDataSize;
    arr.push(pageNum);
    arr.push(Number(pageDataSize));
    pageNum = stuDao.stuDao(
      "SELECT s_id,s_name,s_age,s_sex,s_date,s_phone,s_grade,c_name FROM t_student JOIN t_class on s_cid = c_id order by s_id limit ?,?",
      arr,
      function (err, data) {
        if (data) {
          data = formatData(data);
          res.send(data);
        }
      }
    );
  },
};
